package com.zrrd.jt_admin.manager.controller;

import com.zrrd.jt_admin.common.entity.Manager;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.utils.ContextHolder;
import com.zrrd.jt_admin.common.page.ManagerPageVo;
import com.zrrd.jt_admin.common.vo.ManagerVo;
import com.zrrd.jt_admin.manager.service.ManagerService;
import com.zrrd.jt_admin.manager.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author WanJl
 * @version 1.0
 * @title AdminController
 * @description
 * @create 2024/6/17
 */
@Api(tags = "管理员管理")    //指定在线文档中Controller的名字
@ApiResponses({
        @ApiResponse(code = 500, message = "服务器内部错误，请联系管理人员！"),
        @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@RestController
@RequestMapping("/admin/manager")
@Slf4j
public class ManagerController {

    @Resource
    private ManagerService managerService;

    @Resource
    private RoleService roleService;



    //http://localhost:80891/admin/:page?limit=[:limit]&keyword=[:keyword]
    //http://localhost:80891/admin/1?limit=5&keyword=qw

    @ApiOperation(value = "分页查询|搜索", notes = "分页查询|搜索")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") int page,
                                     @RequestParam(value = "limit",required = false)Integer limit,
                                     @RequestParam(value = "keyword",required = false)String keyword) {

        ManagerPageVo managerPageVo = managerService.findByPage(page, limit, keyword);


        return ResponseResult.queryResponse(managerPageVo);

    }
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @GetMapping("/{id}/one")
    @ResponseBody
    public ResponseResult findOne(@PathVariable("id") Integer id) {
        Long aId = ContextHolder.getId();
        log.info("id为："+aId+"的管理员访问了findOne方法.....");

        Manager manager = managerService.getById(id);
        if (manager == null)
            return ResponseResult.queryResponse(null);
        ManagerVo mv=new ManagerVo();
        //复制属性
        BeanUtils.copyProperties(manager,mv);
        //根据角色id查询角色信息
        mv.setRole(roleService.getById(mv.getRoleId()));
        return ResponseResult.queryResponse(mv);
    }

    @ApiOperation(value = "添加", notes = "添加管理员用户信息")
    @PostMapping
    @ResponseBody
    public ResponseResult add(@RequestBody Manager manager) {
        boolean flag = managerService.save(manager);
        return ResponseResult.addResponse(flag);
    }


    @ApiOperation(value = "修改", notes = "修改管理员用户信息")
    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody Manager manager) {
        boolean flag = managerService.updateById(manager);
        return ResponseResult.updateResponse(flag);
    }


    @ApiOperation(value = "删除", notes = "根据id删除")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult remove(@PathVariable("id") Integer id) {
        boolean flag = managerService.removeById(id);
        return ResponseResult.removeResponse(flag);
    }


    @ApiOperation(value = "修改状态", notes = "根据id修改状态")
    @PutMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult setStatus(@PathVariable("id") Integer id,Integer status) {
        Boolean bStatus=status==1?true:false;

        boolean flag = managerService.lambdaUpdate()//使用lambda形式的修改
                .eq(Manager::getId, id) //比较id
                .set(Manager::getStatus, bStatus)//进行修改
                .update();//执行修改

        /*
            UpdateWrapper<Manager> uw=new UpdateWrapper<>();
            uw.eq("id",id);
            uw.set("status",status);
            boolean flag = managerService.update(uw);
        */

        return ResponseResult.updateResponse(flag);
    }
}
